#!/usr/bin/python2.5
# -*- coding: utf-8 -*-
#
# Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import tipfy
import time

from tipfy.ext.i18n import _
from tipfy.ext import wtforms
from tipfy.ext.wtforms import fields
from tipfy.ext.wtforms import validators

from apps.pf_mobile import handlers
from apps.pf_mobile import pfapi

class AddNoteForm(wtforms.Form):
  person_id = fields.HiddenField(u'Person Id')
  first_name = fields.HiddenField(u'First Name')
  last_name = fields.HiddenField(u'Last Name')
  text = fields.TextAreaField(_(u'状況'), validators=[validators.Required()])
  author_name = fields.TextField(_(u'あなたの名前'),
                                 validators=[validators.Required()])
  author_email = fields.TextField(_(u'メールアドレス'),
                                  validators=[validators.Optional(),
                                              validators.Email()])
  belived_alive = fields.BooleanField(_(u'生存を確認'))

class AddNoteHandler(handlers.BaseHandler):
  def get(self, **kwargs):
    self.form.person_id.data = (self.request.args.get('id') or
                                kwargs.get('id', ''))
    self.form.first_name.data = (self.request.args.get('first_name') or
                                 kwargs.get('first_name', ''))
    self.form.last_name.data = (self.request.args.get('last_name') or
                                kwargs.get('last_name', ''))
    return self.render_response('add_note.html',
                                form=self.form,
                                error=kwargs.get('error', ''))

  def post(self, **kwargs):
    if not self.form.validate():
      return self.get(id=self.form.person_id.data,
                      first_name=self.form.first_name.data,
                      last_name=self.form.last_name.data,
                      error=_(u'必須項目を入力してください'))

    api = pfapi.PfApi()
    person_id = self.form.person_id.data
    note = pfapi.Note(person_record_id=person_id,
                      text=self.form.text.data,
                      author_name=self.form.author_name.data)
    if self.form.belived_alive.data:
      note.found = True
      note.status = 'believed_alive'
    try:
      result = api.create(notes=[note.to_dict()])
    except pfapi.PfApiError:
      return self.get(id=person_id,
                      first_name=self.form.first_name.data,
                      last_name=self.form.last_name.data,
                      error=_(u'エラーが発生しました'))
    email_added = '0'
    if self.form.author_email.data:
      api.subscribe(person_id, self.form.author_email.data)
      email_added = '1'
    return self.redirect_to('view',
                            query_args={'id': self.form.person_id.data,
                                        'added': '1',
                                        'email_added': email_added})

  @tipfy.cached_property
  def form(self):
    return AddNoteForm(self.request)
